package org.libra {
	import flash.display.Shape;
	import flash.events.Event;
	import flash.events.FocusEvent;
	import flash.events.MouseEvent;
	import flash.events.TextEvent;
	import flash.text.TextFieldType;
	import org.libra.basic.BasicTextComponent;
	import org.libra.events.LibraEvent;
	
	/**
	 * 输入框组件
	 * @author zhangzuoqiang
	 */
	public class JTextField extends BasicTextComponent {
		
		/**
		 * 绘制焦边框的Shape
		 */
		private var focusShape:Shape;
		private var drawFocusShape:Boolean;
		
		/**
		 * 构造函数
		 * @param	text 输入框中的内容
		 */
		public function JTextField(text:String = "") {
			super(text);
			focusShape = new Shape();
			this.addChild(focusShape);
			drawFocusShape = true;
		}
		
		/**
		 * 初始化输入框
		 * @param	text
		 */
		override protected function initTextField(text:String):void {
			super.initTextField(text);
			textField.type = TextFieldType.INPUT;
			setMouseWheelEnabled(false);
		}
		
		/**
		 * 重写添加到舞台后的方法
		 * @param	e
		 */
		override protected function onAddToStageHandler(e:Event):void {
			super.onAddToStageHandler(e);
			addEventListener(MouseEvent.MOUSE_DOWN, onMouseDownHandler);
			textField.addEventListener(FocusEvent.FOCUS_IN, onFocusInHandler);
			textField.addEventListener(FocusEvent.FOCUS_OUT,onFocusOutHandler);
		}
		
		/**
		 * 重写从舞台上移除后的方法
		 * @param	e
		 */
		override protected function onRemoveFromStageHandler(e:Event):void {
			super.onRemoveFromStageHandler(e);
			removeEventListener(MouseEvent.MOUSE_DOWN, onMouseDownHandler);
			textField.removeEventListener(FocusEvent.FOCUS_IN, onFocusInHandler);
			textField.removeEventListener(FocusEvent.FOCUS_OUT,onFocusOutHandler);
		}
		
		/**
		 * 鼠标按下时，取消因获得焦点而生成的焦点边框，
		 * @param	e
		 */
		protected function onMouseDownHandler(e:MouseEvent):void {
			focusShape.graphics.clear();
		}
		
		/**
		 * 当失去焦点后，取消因获得焦点而生成的焦点边框。
		 * @param	e
		 */
		private function onFocusOutHandler(e:FocusEvent):void {
			if (drawFocusShape) {
				focusShape.graphics.clear();
			}
			dispatchEvent(new LibraEvent(LibraEvent.LOST_FOCUS));
		}
		
		/**
		 * 当获得焦点后，绘制焦点边框。
		 * @param	e
		 */
		private function onFocusInHandler(e:FocusEvent):void {
			if (drawFocusShape) {
				focusShape.graphics.clear();
				focusShape.graphics.lineStyle(.5, 0xff0000);
				focusShape.graphics.moveTo(0, 0);
				focusShape.graphics.lineTo(getWidth(), 0);
				focusShape.graphics.lineTo(getWidth(), getHeight());
				focusShape.graphics.lineTo(0, getHeight());
				focusShape.graphics.lineTo(0, 0);
			}
			dispatchEvent(new LibraEvent(LibraEvent.GET_FOCUS));
		}
		
		public function setDrawFocusShape(value:Boolean):void {
			this.drawFocusShape = value;
		}
		
		/**
		 * 设置输入框为密码输入模式，这样输入文字后会将文字隐藏
		 * @param	b
		 */
		public function setDisplayAsPassword(b:Boolean):void{
			textField.displayAsPassword = b;
		}
		
		/**
		 * 获取输入框是否是密码输入模式
		 * @return
		 */
		public function isDisplayAsPassword():Boolean{
			return textField.displayAsPassword;
		}
		
		/**
		 * 添加输入框输入事件的监听
		 * @param	listener
		 * @param	useCapture
		 * @param	priority
		 * @param	useWeakReference
		 */
		public function addInputListener(listener:Function, useCapture:Boolean = false, priority:int = 0, useWeakReference:Boolean = false):void { 
			this.textField.addEventListener(TextEvent.TEXT_INPUT, listener, useCapture, priority, useWeakReference);
		}
		
		/**
		 * 移除输入框输入事件的监听
		 * @param	listener
		 * @param	useCapture
		 */
		public function removeInputListener(listener:Function, useCapture:Boolean = false):void { 
			this.textField.removeEventListener(TextEvent.TEXT_INPUT, listener, useCapture);
		}
		
		public function isFocusOwner():Boolean {
			return stage ? stage.focus == this.textField : false;
		}
		
		public function requestFocus():void {
			if (stage) {
				if (stage.focus != this.textField) {
					stage.focus = this.textField;
				}
			}
		}
		
		public function lostFocus():void {
			if (isFocusOwner()) {
				stage.focus = null;
			}
		}
		
		/**
		 * 获取string表达式
		 * @return
		 */
		override public function toString():String {
			return "JTextField:text = " + getText();
		}
		
	}

}